//
//  BLBaseRequest.m
//  BasketBallLeagues
//
//  Created by kenshin on 4/24/17.
//  Copyright © 2017 kenshin. All rights reserved.
//

#import "BLBaseRequest.h"
#import "BLServerConfig.h"

@implementation BLModel

+ (BOOL)propertyIsOptional:(NSString *)propertyName
{
    return YES;
}

- (NSDictionary *)filteredDictionary
{
    NSDictionary *params = [self toDictionary];
    if (params.count < 1) {
        return params;
    }
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    for (NSString *key in params.allKeys) {
        id value = params[key];
        if (value && [value isKindOfClass:[NSNull class]]) {
            continue;
        }
        [parameters setValue:value forKey:key];
    }
    return parameters;
}

@end

@implementation TKBaseResult

//+ (JSONKeyMapper *)keyMapper
//{
//    return [[JSONKeyMapper alloc] initWithModelToJSONDictionary:@{
//                                                                  @"list": @"result.list",
//                                                                  @"info": @"result.info",
//                                                                  @"extra": @"result.extra",
//                                                                  }];
//}

@end

@implementation BLBaseResponse

+ (NSDictionary *)baseKeyToMapper {
    return @{
      @"extra": @"result.extra",
      @"info": @"result.info",
      @"list": @"result.list"
      };
}

+ (JSONKeyMapper *)keyMapper
{
    return [[JSONKeyMapper alloc] initWithModelToJSONDictionary:[self baseKeyToMapper]];
}


- (BOOL)isSuccess {
    return self.errcode == BLAPIErrorCodeSuccess;
}

@end


@implementation BLBaseRequest


- (instancetype)init
{
    if (self = [super init]) {
        self.device = 1;
        self.curtime = [PublicTool getNowTimeStamp];
        self.os_version = [SystemHelper osVersion];
        self.version = [SystemHelper appVersion];
        //暂时写死
        self.user_id = @"0";
        //拼接sign
        [self configSign];
        
        if ([TKAccountManager isLogin]) {
            self.user_id = [TKAccountManager sharedManager].accountInfo.result.info.user_id;
            [self configSign];
        }
        
    }
    return self;
}

- (void)configSign {
    NSString *sign = [NSString stringWithFormat:@"%@%@%@",self.user_id,self.curtime,@"yichangyanchu"];
    DDLogInfo(@"%@",sign);
    NSString *result = [sign jc_md5];
    self.sign = [result lowercaseString];
}
- (KSRequestMethod)requestMethod
{
    return KSRequestMethodPOST;
}

- (NSTimeInterval)requestTimeoutInterval
{
    return 30;
}

- (NSString *)requestUrl
{
    return self.url;
}

- (NSString *)baseUrl
{
    return [BLServerConfig sharedInstance].baseUrl;
}

- (NSDictionary *)parameters {
    if ([TKAccountManager isLogin]) {
//        self.token = [BLAccountManager sharedManager].accountInfo.token;
//        self._t = [BLAccountManager sharedManager].accountInfo._t;
    }
    return [super parameters];
}

@end
